﻿#include "PhysicsList.hh"

#include <stdio.h>
#include <stdlib.h>

#include <memory>

#include "G4DecayPhysics.hh"
#include "G4EmExtraPhysics.hh"
#include "G4EmStandardPhysics.hh"
#include "G4EmStandardPhysics_option3.hh"
#include "G4EmStandardPhysics_option4.hh"
//#include "G4HadronElasticPhysics.hh"
// #include "G4HadronPhysicsQGSP_BIC_HP.hh"
// #include "QBBC.hh"
// #include "G4HadronInelasticQBBC.hh"

#include "G4AutoLock.hh"
#include "G4EmStandardPhysics.hh"
#include "G4HadronElasticPhysicsXS.hh"
#include "G4HadronInelasticQBBC.hh"
#include "G4IonBinaryCascadePhysics.hh"
#include "G4IonElasticPhysics.hh"
#include "G4IonPhysics.hh"
#include "G4LossTableManager.hh"
#include "G4NeutronTrackingCut.hh"
#include "G4StoppingPhysics.hh"
#include "G4SystemOfUnits.hh"
#include "G4Threading.hh"
#include "MyMacros.hh"
#include "PhysicsListMessenger.hh"
#include "Util.hh"
// #include "G4HadronPhysicsQGSP_BIC.hh"
// #include "MyMacros.hh"
// #include "G4Electron.hh"
// #include "G4Gamma.hh"
// #include "G4HadronElasticPhysicsHP.hh"
// #include "G4HadronElasticPhysicsXS.hh"
// #include "G4IonPhysicsXS.hh"
// #include "G4ParticleTable.hh"
// #include "G4ParticleTypes.hh"
// #include "G4Positron.hh"
// #include "G4ProcessManager.hh"
// #include "G4Proton.hh"
// #include "G4RadioactiveDecayPhysics.hh"
// #include "G4AutoDelete.hh"
using std::make_unique;

namespace
{
    G4Mutex constructProcessMutex = G4MUTEX_INITIALIZER;
    constexpr G4int phyVer = 1;  // version
}  // namespace

//================ only cons in master thread
PhysicsList::PhysicsList()
{
    MYG4TID(tid);
    //DINFOLN("``````````````````````````````````[tid {} PhysicsList/PhysicsList]", tid);

    // given verbose level is set to all physics constructors. 越高越复杂.
    SetVerboseLevel(1);
    _messenger = make_unique<PhysicsListMessenger>(this);

    G4LossTableManager::Instance();
    this->defaultCutValue = 0.05 * mm;

    _fHadronPhys.clear();

    _fDecayList = make_unique<G4DecayPhysics>(phyVer);
    _fEmPhysicsList = make_unique<G4EmStandardPhysics>(phyVer);
    // _fEmPhysicsList = new G4EmStandardPhysics_option3(phyVer);
    // _fEmPhysicsList = make_unique<G4EmStandardPhysics_option4>(phyVer);
}

PhysicsList::~PhysicsList()
{  //
    Util::Clear(_fHadronPhys);
}

void PhysicsList::ConstructParticle()
{
    MYG4TID(tid);
    //DINFOLN("[tid {} PhysicsList] ConstructParticle()", tid);
    _fDecayList->ConstructParticle();
}

/// master, worker 都会执行
void PhysicsList::ConstructProcess()
{
    G4AutoLock l(&constructProcessMutex);  // Protection to be removed (A.Dotti)

    MYG4TID(tid);
    //DINFOLN("[tid {} PhysicsList/ConstructProcess]", tid);

    /// 必须手动调用这个过程, G4VModularPhysicsList
    AddTransportation();

    _fDecayList->ConstructProcess();
    _fEmPhysicsList->ConstructProcess();

    for (size_t i = 0; i < _fHadronPhys.size(); i++)
    {
        _fHadronPhys[i]->ConstructProcess();
    }
}

/**
s:Ph/Default/Type = "Geant4_Modular"
sv:Ph/Default/Modules = 6 "g4em-standard_opt4" "g4h-phy_QGSP_BIC_HP" "g4decay" "g4ion-binarycascade" "g4h-elastic_HP" "g4stopping"
d:Ph/Default/EMRangeMin = 100. eV
d:Ph/Default/EMRangeMax = 500. MeV
*/

void PhysicsList::AddPhysicsList(const G4String& name)
{
    if (verboseLevel > 0)
    {
        MYG4TID(tid);
        LINFO("```[tid {} PhysicsList/AddPhysicsList] name = {}", tid, name)
    }

    //============= add hadron, ion etc.
    bool addOthers = false;
    bool addIBC = false;

    if (name == "em")
    {
        _fEmPhysicsList = make_unique<G4EmStandardPhysics>(phyVer);
        addOthers = true;
    }
    else if (name == "em_op3")
    {
        _fEmPhysicsList = make_unique<G4EmStandardPhysics_option3>(phyVer);
        addOthers = true;
    }
    else if (name == "em_op4")
    {
        _fEmPhysicsList = make_unique<G4EmStandardPhysics_option4>(phyVer);
        addOthers = true;
    }
    else  ///================================= default
    {
        _fEmPhysicsList = make_unique<G4EmStandardPhysics>(phyVer);
        addOthers = true;
    }

    if (name == "QBBC")
    {
        addOthers = true;
    }
    if (name == "QBBC_IBC")  // ion binary cascade
    {
        addOthers = true;
        addIBC = true;
    }

    if (addOthers)
    {
        _fHadronPhys.clear();

        _fHadronPhys.reserve(16);

        _fHadronPhys.push_back(new G4EmExtraPhysics(phyVer));
        // _fDecayList 设置为 G4DecayPhysics(phyVer);
        _fHadronPhys.push_back(new G4HadronElasticPhysicsXS(phyVer));

        _fHadronPhys.push_back(new G4StoppingPhysics(phyVer));
        _fHadronPhys.push_back(new G4IonPhysics(phyVer));
        _fHadronPhys.push_back(new G4IonElasticPhysics(phyVer));

        _fHadronPhys.push_back(new G4HadronInelasticQBBC(phyVer));

        _fHadronPhys.push_back(new G4NeutronTrackingCut(phyVer));
        /// TOPAS 添加
        if (addIBC)
        {
            _fHadronPhys.push_back(new G4IonBinaryCascadePhysics(phyVer));
        }

        _fHadronPhys.shrink_to_fit();
    }
    else
    {
        LINFO("```[PhysicsList/AddPhysicsList] name `{}` is not defined", name)
    }
}

void PhysicsList::SetCuts()
{
    G4VUserPhysicsList::SetCuts();  //将粒子的截断设置为默认
}